/*
 * CRSA.h
 *
 *  Created on: 2009-5-17
 *      Author: thh
 */

#ifndef CRSA_H_20090517
#define CRSA_H_20090517

#include <openssl/rsa.h>
#include <openssl/objects.h>
#include <string>

namespace Ybdj {
namespace Crypt{

class RSA {
public:
	enum Padding{
		PKCS1_PADDING=RSA_PKCS1_PADDING,
		PKCS1_OAEP_PADDING=RSA_PKCS1_OAEP_PADDING,
		SSLV23_PADDING=RSA_SSLV23_PADDING,
		NO_PADDING=RSA_NO_PADDING
	};
	enum Digest{
		SHA1 = NID_sha1,
		RIPEMD160 = NID_ripemd160,
		MD5 = NID_md5,
		MD5_SHA1 = NID_md5_sha1,
	};
private:
	::RSA *rsa;

public:
	RSA(unsigned int bits,unsigned int e);
	virtual ~RSA();

	bool Check();
	int Size();

	int PublicEncrypt(int flen, unsigned char *from,unsigned char *to, Padding padding=RSA::PKCS1_PADDING);
	int PublicDecrypt(int flen, unsigned char *from,unsigned char *to, Padding padding=RSA::PKCS1_PADDING);
	int PrivateEncryt(int flen, unsigned char *from,unsigned char *to, Padding padding=RSA::PKCS1_PADDING);
	int PrivateDecrypt(int flen, unsigned char *from,unsigned char *to, Padding padding=RSA::PKCS1_PADDING);
	void Sign(Digest type, unsigned char *m, unsigned int m_len,unsigned char *sigret, unsigned int *siglen);
	bool Verify(Digest type, unsigned char *m, unsigned int m_len,unsigned char *sigbuf, unsigned int siglen);

	void BlindingOn(BN_CTX *ctx);
	void BlindingOff(BN_CTX *ctx);

	std::string toString();
};

}
}

#endif /* CRSA_H_20090517 */
